//
//  HMBasicViewController.swift
//  01-SinaWeibo
//
//  Created by 周英培 on 16/5/21.
//  Copyright © 2016年 周英培. All rights reserved.
//

import UIKit
/*
 1.系统的返回按钮不存在右滑返回手势失效，自定义的返回按钮存在右滑返回手势失效问题
 */

/*
 
 问题一：var backTitle = "返回"
 
 if childViewControllers.count > 0 {
 
 if childViewControllers.count == 1 {
 
 backTitle = childViewControllers.first?.title ?? "返回"    这一句什么意思
 }
 let backItem = UIBarButtonItem(imageName: "navigationbar_back_withtext", title: backTitle, target: self, action: #selector(back))
 
 viewController.navigationItem.leftBarButtonItem = backItem

 */
class HMBasicViewController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //系统的返回按钮不存在右滑返回手势失效，自定义的返回按钮存在右滑返回手势失效问题
        //解决这一个问题的方法如下
        //只写这句代码存在一个问题，就是会到最顶层视图时，还能右滑手势返回，并且返回之后就会出现问题
        //解决这一问题，需要实现手势协议的方法
        self.interactivePopGestureRecognizer?.delegate = self
        
        
    }
    
    //重写puch方法
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        //MARK:解决第一个跳转的控制器的返回按钮的文字是最顶层控制器的标题，其他的控制器的返回按钮都是返回文字
        //设置左边返回按钮
        //判断 最顶层没有返回按钮的文字，第一层返回按钮的文字是最顶层控制器的标题，其他的返回按钮是返回文字
        var backTitle = "返回"
        
        if childViewControllers.count > 0 {
            
            if childViewControllers.count == 1 {
                
                backTitle = childViewControllers.first?.title ?? "返回"
                
            }
            
            //跳转到子控制器时，隐藏tabBar
            viewController.hidesBottomBarWhenPushed = true

            let backItem = UIBarButtonItem(imageName: "navigationbar_back_withtext", title: backTitle, target: self, action: #selector(back))
            
            viewController.navigationItem.leftBarButtonItem = backItem
        }
        
        super.pushViewController(viewController, animated: animated)
        
    }

    //手势协议方法
    //return true 表示返回手势能够正常进行
    //return false 表示返回手势不能执行
    @objc internal func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        
//        if childViewControllers.count > 1 {
//            
//            return true
//        }
//        
//        return false
        
        return childViewControllers.count > 1
    }
    
    @objc private func back() {
        //这里不能用下面这一句，因为这个文件本身就是navigationController
//        navigationController?.popViewControllerAnimated(true)
         popViewControllerAnimated(true)
        
    }
    
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
